package command;

public class CommandPattern {

    public static void main(String[] args) {
        // 遥控器
        RemoteControl remote = new RemoteControl();

        // 电灯
        Light livingRoomLight = new Light("Living Room");
        Light kitchenLight = new Light("Kitchen");
        // 电风扇
        CeilingFan livingRoomCeilingFan = new CeilingFan("Living Room");
        // 车库门
        GarageDoor garageDoor = new GarageDoor();
        // 音响
        Stereo livingRoomStereo = new Stereo("Living Room");

        // 一堆开/关命令
        LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);
        LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);
        LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight);
        LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight);

        CeilingFanOnCommand livingRoomCeilingFanOn = new CeilingFanOnCommand(livingRoomCeilingFan);
        CeilingFanOffCommand livingRoomCeilingFanOff = new CeilingFanOffCommand(livingRoomCeilingFan);

        GarageDoorOnCommand garageDoorOn = new GarageDoorOnCommand(garageDoor);
        GarageDoorDownCommand garageDoorDown = new GarageDoorDownCommand(garageDoor);

        StereoOnWithCDCommand livingRoomStereoOnWithCD = new StereoOnWithCDCommand(livingRoomStereo);
        StereoOffCommand livingRoomStereoOff = new StereoOffCommand(livingRoomStereo);

        // 设置遥控器的每个按钮
        remote.setCommand(0, livingRoomLightOn, livingRoomLightOff);
        remote.setCommand(1, kitchenLightOn, kitchenLightOff);
        remote.setCommand(2, livingRoomCeilingFanOn, livingRoomCeilingFanOff);
        remote.setCommand(3, garageDoorOn, garageDoorDown);
        remote.setCommand(4, livingRoomStereoOnWithCD, livingRoomStereoOff);

        System.out.println(remote);

        remote.onButtonWasPushed(0);
        remote.offButtonWasPushed(0);
        remote.onButtonWasPushed(1);
        remote.offButtonWasPushed(1);
        remote.onButtonWasPushed(2);
        remote.offButtonWasPushed(2);
        remote.onButtonWasPushed(3);
        remote.offButtonWasPushed(3);
        remote.onButtonWasPushed(4);
        remote.offButtonWasPushed(4);

        System.out.println(remote);

        remote.undoButtonWasPushed();

        System.out.println("====================");

        // 风扇状态测试
        RemoteControl remoteWithUndo = new RemoteControl();

        CeilingFan ceilingFan = new CeilingFan("Living Room");

        CeilingFanLowCommand ceilingFanLow = new CeilingFanLowCommand(ceilingFan);
        CeilingFanMediumCommand ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan);
        CeilingFanHighCommand ceilingFanHigh = new CeilingFanHighCommand(ceilingFan);
        CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan);

        remoteWithUndo.setCommand(0, ceilingFanLow, ceilingFanOff);
        remoteWithUndo.setCommand(1, ceilingFanMedium, ceilingFanOff);
        remoteWithUndo.setCommand(2, ceilingFanHigh, ceilingFanOff);

        remoteWithUndo.onButtonWasPushed(1);
        remoteWithUndo.offButtonWasPushed(1);
        System.out.println(remoteWithUndo);
        remoteWithUndo.undoButtonWasPushed();

        remoteWithUndo.onButtonWasPushed(2);
        System.out.println(remoteWithUndo);
        remoteWithUndo.undoButtonWasPushed();

        System.out.println("====================");

        // 宏命令
        RemoteControl remoteControl = new RemoteControl();

        Light light = new Light("Living Room");
        TV tv = new TV("Living Room");
        Stereo stereo = new Stereo("Living Room");
        HotTub hotTub = new HotTub();

        LightOnCommand lightOn = new LightOnCommand(light);
        TVOnCommand tvOn = new TVOnCommand(tv);
        StereoOnWithCDCommand stereoOn = new StereoOnWithCDCommand(stereo);
        HotTubOnCommand hotTubOn = new HotTubOnCommand(hotTub);

        LightOffCommand lightOff = new LightOffCommand(light);
        TVOffCommand tvOff = new TVOffCommand(tv);
        StereoOffCommand stereoOff = new StereoOffCommand(stereo);
        HotTubOffCommand hotTubOff = new HotTubOffCommand(hotTub);

        Command[] partyOn = {lightOn, tvOn, stereoOn, hotTubOn};
        Command[] partyOff = {lightOff, tvOff, stereoOff, hotTubOff};

        MacroCommand partyOnMacro = new MacroCommand(partyOn);
        MacroCommand partyOffMacro = new MacroCommand(partyOff);

        remoteControl.setCommand(0, partyOnMacro, partyOffMacro);
        System.out.println(remoteControl);
        System.out.println("--- Pushing Macro On ---");
        remoteControl.onButtonWasPushed(0);
        System.out.println("--- Pushing Macro Off ---");
        remoteControl.offButtonWasPushed(0);
        System.out.println(remoteControl);
        remoteControl.undoButtonWasPushed();
    }

}
